package frame;


import pojo.UserDO;
import service.UserService;
import service.impl.UserServiceImpl;
import util.Util;
import util.*;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Changemoney extends JPanel implements ActionListener {
    private Atm atm;
    private Icon sureImg = Util.getIcon("/buttonSure.jpg");
    private Icon offImg = Util.getIcon("/buttonOff.jpg");
    private JButton sureBtn = new JButton(sureImg);
    private JButton offBtn = new JButton(offImg);
    private JTextField changename = new JTextField();
    private JTextField changemoney = new JTextField();
    private UserService userService = new UserServiceImpl();

    public Changemoney(Atm atm) {
        this.atm = atm;
        offBtn.setBounds(930,200,140,50);
        sureBtn.setBounds(680,200,140,50);
        changename.setBounds(430,200,240,50);
        changemoney.setBounds(430,300,240,50);
        //添加组件
        this.add(offBtn);
        this.add(sureBtn);
        this.add(changename);
        this.add(changemoney);

        //监听绑定
        offBtn.addActionListener(this);
        sureBtn.addActionListener(this);

        this.setLayout(null);
        this.setOpaque(false);

    }


    @Override
    public void actionPerformed(ActionEvent e) {
        UserDO use = atm.getUser();
        if (e.getSource() == offBtn) {
            atm.switchPanel("menu");
        }
        if (e.getSource()==sureBtn){
            try {
                userService.exchangMoney(use.getId(),changename.getText(),Integer.parseInt(changemoney.getText()));
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            System.out.println(changename.getText());

        }


    }
}
